
package managedbean.CRM.ProductPrice;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.CRM.ProductPriceBeanLocal;

@ManagedBean(name="addProductPriceManagerBean")
@ViewScoped
public class AddProductPriceManagerBean implements Serializable {

    @EJB
    private ProductPriceBeanLocal productPriceBeanLocal;
    private String productType;
    private int lowerLimit;
    private int higherLimit;
    private double productPrice;
    private boolean success;
    private String statusMessage;
    
    public AddProductPriceManagerBean() {
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getHigherLimit() {
        return higherLimit;
    }

    public void setHigherLimit(int higherLimit) {
        this.higherLimit = higherLimit;
    }

    public int getLowerLimit() {
        return lowerLimit;
    }

    public void setLowerLimit(int lowerLimit) {
        this.lowerLimit = lowerLimit;
    }
    
    
    public void saveNewProductPrice(ActionEvent event) 
    { 
        try 
        {
            if (productPriceBeanLocal.checkConflict(-1, productType, lowerLimit, higherLimit))
            {
                statusMessage = "Overlapping range limit. Please try again.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Unsuccessful:" + statusMessage + "", ""));
            }
            else
            {
                productPriceBeanLocal.createProductPrice(productType, lowerLimit, higherLimit, productPrice);
                statusMessage = "New Product of Type "+productType+" with price of "+productPrice+" Saved Successfully";

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Discount Result: " + statusMessage, ""));
            }

            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }   
}
